﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ICAM.View
{
    /// <summary>
    /// Paginator.xaml 的交互逻辑
    /// </summary>
    public partial class Paginator : UserControl,INotifyPropertyChanged
    {
       
        public Paginator()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        #region variables
        private const int _ItemCount = 5;//固定显示5个按钮
        private const string _slh = "...";//省略号
        #endregion

        #region DependencyProperty Register

        /// <summary>
        /// 数据总计页数
        /// </summary>
        public static readonly DependencyProperty DataCountProperty = DependencyProperty.Register("DataCount", typeof(int), typeof(Paginator), new FrameworkPropertyMetadata(0, new PropertyChangedCallback(OnDataCountChanged)));
        /// <summary>
        /// 当前页
        /// </summary>
        public static readonly DependencyProperty CurrentPageProperty = DependencyProperty.Register("CurrentPage", typeof(int), typeof(Paginator), new PropertyMetadata(1, new PropertyChangedCallback(OnCurrentPageChanged)));


        #endregion


        private ObservableCollection<int> _paginatorModel = new ObservableCollection<int>();

        public ObservableCollection<int> PaginatorModel
        {
            get { return this._paginatorModel; }
            set {
                this._paginatorModel = value;
                OnPropertyChanged("PaginatorViewModel");
            }
        }

        private int _paginatorSelectedModel = 1;

        public int PaginatorSelectedModel
        {
            get { return this._paginatorSelectedModel; }
            set {
                this._paginatorSelectedModel = value;
                OnPropertyChanged("PaginatorSelectedModel");

                CurrentPage = PaginatorSelectedModel;
            }
        }


        public int DataCount
        {
            get { return (int)GetValue(DataCountProperty); }
            set { SetValue(DataCountProperty, value); }
        }


        private static void OnDataCountChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            //Paginator Paginator = sender as Paginator;
            //Paginator.Measure();
            Paginator Paginator = sender as Paginator;
            Paginator.InitControl();
        }



        public int CurrentPage
        {
            get { return (int)GetValue(CurrentPageProperty); }
            set { SetValue(CurrentPageProperty, value); }
        }

        #region  export event    

        public static readonly RoutedEvent PaginatorSelectedEvent = EventManager.RegisterRoutedEvent("PaginatorSelected", RoutingStrategy.Bubble, typeof(RoutedPropertyChangedEventHandler<int>), typeof(Paginator));
        public event RoutedPropertyChangedEventHandler<int> PaginatorSelected
        {
            add { AddHandler(PaginatorSelectedEvent, value); }
            remove { RemoveHandler(PaginatorSelectedEvent, value); }
        }

        private static void OnCurrentPageChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            Paginator Paginator = sender as Paginator;
            int oldValue = (int)args.OldValue;
            int newValue = (int)args.NewValue;
 
            //引发事件
            Paginator.RaiseEventPaginatorSelected(newValue, oldValue);

        }

        private void RaiseEventPaginatorSelected(int newValue, int oldValue)
        {
            RoutedPropertyChangedEventArgs<int> args = new RoutedPropertyChangedEventArgs<int>(newValue, oldValue)
            {
                RoutedEvent = PaginatorSelectedEvent
            };
            RaiseEvent(args);
        }
        #endregion


        #region Measure

        public void  InitControl()
        {
            PaginatorModel.Clear();
            for (int i = 1; i <=_ItemCount; i++)
            {
                PaginatorModel.Add(i);
            }
            PaginatorSelectedModel = PaginatorModel[0];
            
            if (DataCount > _ItemCount)
            {
                FirstPage.Visibility = Visibility.Collapsed;
                PrePage.Visibility = Visibility.Collapsed;
                NextPage.Visibility = Visibility.Visible;
                LastPage.Visibility = Visibility.Visible;
            }
            else {
                FirstPage.Visibility = Visibility.Collapsed;
                PrePage.Visibility = Visibility.Collapsed;
                NextPage.Visibility = Visibility.Collapsed;
                LastPage.Visibility = Visibility.Collapsed;
            }
            
        }

        public void SetCurrentPage(int index)
        {

        }

        /// <summary>
        /// 重新绑定数据
        /// </summary>
        private void Measure()
        {
          
        }
        #endregion

        private void NextPageEx()
        {
            //找最后一个
            int iPerLastPage = PaginatorModel[PaginatorModel.Count - 1];
            if (iPerLastPage + _ItemCount >= DataCount)//如果能显示完
            {
                PaginatorModel.Clear();
                for (int i = iPerLastPage + 1; i <= DataCount; i++)
                {
                    PaginatorModel.Add(i);
                }
                PaginatorSelectedModel = PaginatorModel[0];
                //CurrentPage= PaginatorModel[0];
                FirstPage.Visibility = Visibility.Visible;
                PrePage.Visibility = Visibility.Visible;
                NextPage.Visibility = Visibility.Collapsed;
                LastPage.Visibility = Visibility.Collapsed;
            }
            else
            {
                PaginatorModel.Clear();
                for (int i = iPerLastPage + 1; i <= iPerLastPage + _ItemCount; i++)
                {
                    PaginatorModel.Add(i);
                }
                PaginatorSelectedModel = PaginatorModel[0];
                //CurrentPage = PaginatorModel[0];
                FirstPage.Visibility = Visibility.Visible;
                PrePage.Visibility = Visibility.Visible;
                NextPage.Visibility = Visibility.Visible;
                LastPage.Visibility = Visibility.Visible;
            }
        }

        private void PrePageEx()
        {
            //找第一个
            int iPerFirstPage = PaginatorModel[0];
            if (iPerFirstPage - _ItemCount <= 1)//如果能显示完
            {
                PaginatorModel.Clear();
                for (int i = 1; i <= _ItemCount; i++)
                {
                    PaginatorModel.Add(i);
                }
                PaginatorSelectedModel = PaginatorModel[0];
                //CurrentPage = PaginatorModel[0];
                FirstPage.Visibility = Visibility.Collapsed;
                PrePage.Visibility = Visibility.Collapsed;
                NextPage.Visibility = Visibility.Visible;
                LastPage.Visibility = Visibility.Visible;
            }
            else
            {
                PaginatorModel.Clear();
                for (int i = iPerFirstPage - _ItemCount; i <= iPerFirstPage - 1; i++)
                {
                    PaginatorModel.Add(i);
                }
                PaginatorSelectedModel = PaginatorModel[0];
                //CurrentPage = PaginatorModel[0];
                FirstPage.Visibility = Visibility.Visible;
                PrePage.Visibility = Visibility.Visible;
                NextPage.Visibility = Visibility.Visible;
                LastPage.Visibility = Visibility.Visible;
            }
        }

        private void LastPageEx()
        {
            int pageCount = DataCount / _ItemCount;
            int pageRemainder = DataCount % _ItemCount;
            if (pageRemainder == 0)//若能整除
            {
                PaginatorModel.Clear();
                for (int i = ((pageCount - 1) * _ItemCount) + 1; i <= DataCount; i++)
                {
                    PaginatorModel.Add(i);
                }
            }
            else//若不能整除
            {
                PaginatorModel.Clear();
                for (int i = (pageCount * _ItemCount) + 1; i <= DataCount; i++)
                {
                    PaginatorModel.Add(i);
                }
            }
            PaginatorSelectedModel = PaginatorModel[PaginatorModel.Count-1];
            //CurrentPage = PaginatorModel[PaginatorModel.Count - 1];
            FirstPage.Visibility = Visibility.Visible;
            PrePage.Visibility = Visibility.Visible;
            NextPage.Visibility = Visibility.Collapsed;
            LastPage.Visibility = Visibility.Collapsed;
        }

        private void FirstPageEx()
        {
            PaginatorModel.Clear();
            for (int i = 1; i <= _ItemCount; i++)
            {
                PaginatorModel.Add(i);
            }
            PaginatorSelectedModel = PaginatorModel[0];
            //CurrentPage = PaginatorModel[0];
            FirstPage.Visibility = Visibility.Collapsed;
            PrePage.Visibility = Visibility.Collapsed;
            NextPage.Visibility = Visibility.Visible;
            LastPage.Visibility = Visibility.Visible;
        }

        private void PageButton_Click(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            string name = button.Name;
            if (name == "NextPage")
            {
                NextPageEx();
                return;
            }

            if (name == "PrePage")
            {
                PrePageEx();
                return;
            }

            //最后一页
            if (name == "LastPage")
            {

                LastPageEx();
                return;
            }

            //第一页
            if (name == "FirstPage")
            {
                FirstPageEx();
                return;
            }

            if (name == "RadiusNextPage")
            {
                PaginatorSelectedModel += 1;
                if (PaginatorSelectedModel > DataCount)
                {
                    PaginatorSelectedModel = DataCount;
                    return;
                }
                if (PaginatorSelectedModel > PaginatorModel[PaginatorModel.Count - 1])
                {
                    NextPageEx();
                }

                return;
            }

            if (name == "RadiusUpPage")
            {
                PaginatorSelectedModel -=1;
                if (PaginatorSelectedModel < PaginatorModel[0])
                {
                    PrePageEx();
                }
               
                return;
            }


        }


        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raises the PropertyChanged event.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    public class PaginatorModel
    {
        public string ID { get; set; }

        public string Text1 { get; set; }
    }

    public class PaginatorViewModel: INotifyPropertyChanged
    {
        private  int _first = 1;
        public int First
        {
            get { return this._first; }
            set {
                this._first = value;
                OnPropertyChanged("First");
            }
        }

        private int _second = 2;
        public int Second
        {
            get { return this._second; }
            set
            {
                this._second = value;
                OnPropertyChanged("Second");
            }
        }

        private int _three = 3;
        public int Three
        {
            get { return this._three; }
            set
            {
                this._three = value;
                OnPropertyChanged("Three");
            }
        }

        private int _four = 4;
        public int Four
        {
            get { return this._four; }
            set
            {
                this._four = value;
                OnPropertyChanged("Four");
            }
        }

        private int _five = 5;
        public int Five
        {
            get { return this._five; }
            set
            {
                this._five = value;
                OnPropertyChanged("Five");
            }
        }

        private int _six = 6;
        public int Six
        {
            get { return this._six; }
            set
            {
                this._six = value;
                OnPropertyChanged("Six");
            }
        }

        private int _seven = 7;
        public int Seven
        {
            get { return this._seven; }
            set
            {
                this._seven = value;
                OnPropertyChanged("Seven");
            }
        }

        

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Raises the PropertyChanged event.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected virtual void OnPropertyChanged(string propertyName)
        {
            var handler = this.PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
